/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dal;

import entity.EnrollEvent;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hailk_c00639
 */
public class EnrollEventDAL {

    MyConnection con = new MyConnection();

    public List<EnrollEvent> getAllEnrollEvent() {
        List<EnrollEvent> lst = new ArrayList<>();

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_GetAllEnrollEvent}");
            ResultSet rs = call.executeQuery();
            while (rs.next()) {
                EnrollEvent enrollEvent = new EnrollEvent();
                enrollEvent.setEnrollEventID(rs.getInt("EnrollEventID"));
                enrollEvent.setEventID(rs.getInt("EventID"));
                enrollEvent.setAccountID(rs.getInt("AccountID"));
                enrollEvent.setIsWinner(rs.getInt("IsWinner"));

                lst.add(enrollEvent);
            }

        } catch (SQLException ex) {
            Logger.getLogger(EnrollEventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return lst;
    }

    public boolean insertEnroll(EnrollEvent enrollEvent) {
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_InsertEnrollEvent(?,?,?)}");
            call.setInt(1, enrollEvent.getEventID());
            call.setInt(2, enrollEvent.getAccountID());
            call.setInt(3, enrollEvent.getIsWinner());

            call.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(EnrollEventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return true;
    }

    public boolean updateEnroll(EnrollEvent enrollEvent) {
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_UpdateEnrollEvent(?,?,?,?)}");
            call.setInt(1, enrollEvent.getEnrollEventID());
            call.setInt(2, enrollEvent.getEventID());
            call.setInt(4, enrollEvent.getAccountID());
            call.setInt(5, enrollEvent.getIsWinner());

            call.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(EnrollEventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return true;
    }

    public boolean deleteEnrollEventByID(int enrollEventID) {
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_DeleteEnrollEventByID(?)}");
            call.setInt(1, enrollEventID);
            call.execute();

        } catch (SQLException ex) {
            Logger.getLogger(EnrollEventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return true;
    }

    public EnrollEvent searchEnrollEventByID(int enrollEventID) {
        EnrollEvent enrollEvent = null;

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_SearchEnrollEventByID(?)}");
            call.setInt(1, enrollEventID);
            ResultSet rs = call.executeQuery();
            if (rs.next()) {
                enrollEvent = new EnrollEvent();
                enrollEvent.setEventID(rs.getInt("EventID"));
                enrollEvent.setAccountID(rs.getInt("AccountID"));
                enrollEvent.setIsWinner(rs.getInt("IsWinner"));
            }

        } catch (SQLException ex) {
            Logger.getLogger(EnrollEventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }

        return enrollEvent;
    }

    public EnrollEvent searchEnrollEventByEventID(int eventID) {
        EnrollEvent enroll = null;
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_SearchEnrollEventByEventID(?)}");
            call.setInt(1, eventID);
            ResultSet rs = call.executeQuery();
            if (rs.next()) {
                enroll = new EnrollEvent();
                enroll.setEnrollEventID(rs.getInt("EnrollEventID"));
                enroll.setAccountID(rs.getInt("AccountID"));
                enroll.setIsWinner(rs.getInt("Iswinner"));
            }

        } catch (SQLException ex) {
            Logger.getLogger(EnrollEventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }

        return enroll;
    }

    public EnrollEvent searchEnrollEventByAccountID(int accountID) {
        EnrollEvent enroll = null;
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_SearchEnrollEventByAccountID(?)}");
            call.setInt(1, accountID);
            ResultSet rs = call.executeQuery();
            if (rs.next()) {
                enroll = new EnrollEvent();
                enroll.setEnrollEventID(rs.getInt("EnrollEventID"));
                enroll.setEventID(rs.getInt("EventID"));
                enroll.setIsWinner(rs.getInt("IsWinner"));
            }

        } catch (SQLException ex) {
            Logger.getLogger(EnrollEventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }

        return enroll;
    }
}
